package br.com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Aggregator extends LinearLayout {
	private Context context;
	private AggregatorListener listener;
	
	public void setListener(AggregatorListener l) {
		this.listener = l;
	}
	
	public Aggregator(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		inflateMarmota();
	}

	public Aggregator(Context context) {
		super(context);
		this.context = context;
		inflateMarmota();
	}

	private void inflateMarmota() {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.marmota_layout, this);

	}
	
	public Button addButton(int resStringId, final View.OnClickListener listener) {
			final Button button = new Button(context);
			button.setText(context.getText(resStringId));
			button.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					listener.onClick(v);
					Aggregator.this.listener.onButtonClicked(button, Aggregator.this.getTrita());
				}
			});
			
			LinearLayout l = (LinearLayout) this.findViewById(R.idMarmota.llBtnAggregator);
			l.addView(button);
			return button;
	}
	
	public interface AggregatorListener {
		public void onButtonClicked(Button b, TextView t);
	}
	
	private TextView getTrita() {
		return (TextView) this.findViewById(R.idMarmota.tvTrita);
	}

}
